﻿using GreenXi.ViewModel;
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using ZedGraph;

namespace GreenXi.Pages.Power
{

    /// <summary>
    /// Monitoring.xaml 的交互逻辑
    /// </summary>
    public partial class Monitoring : System.Windows.Controls.UserControl
    {
        public static Double X1 = 0;
        public static Double X2 = 0;
        public static Double Y1 = 0;
        public static Double Y2 = 0;

        //记录当前点xy坐标
        private static double oldx = 0;
        private static double oldy = 0;
        //点击的第几个框
        private static int Num = 0;

        //点击的是节点还是内部 
        private static int node = 0;

        //鼠标拖拽状态
        private bool isDragging = false;
        private bool isResizing = false;

        public static MonitoringViewModel Instance { get; set; }
        public Monitoring()
        {
            InitializeComponent();
            Instance = new MonitoringViewModel(this);
            this.DataContext = Instance;

            // 订阅窗口的SizeChanged事件
            this.SizeChanged += Window_SizeChanged;

        }
        //鼠标移动
        private bool zedGraphControl1_MouseMoveEvent(ZedGraphControl sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (isDragging)
            {
                double x1, y1, movex, movey;

                // 将屏幕坐标转换为图像坐标
                sender.GraphPane.ReverseTransform(new PointF(e.X, e.Y), out x1, out y1);
                movex = x1 - oldx; movey = y1 - oldy;
                oldx = x1; oldy = y1;

                if (movex != 0 || movey != 0)
                {

                    ZedGraph.GraphPane myPane = zedGraphControl.GraphPane;
                    if (node == 0 && myPane != null)
                    {
                        for (int i = 0; i <= myPane.CurveList.Count; i++)
                        {

                            if (i < 4 * Num && i >= 4 * (Num - 1))
                            {

                                myPane.CurveList[i].Points[0].X = myPane.CurveList[i].Points[0].X + movex;
                                myPane.CurveList[i].Points[0].Y = myPane.CurveList[i].Points[0].Y + movey;
                                myPane.CurveList[i].Points[1].X = myPane.CurveList[i].Points[1].X + movex;
                                myPane.CurveList[i].Points[1].Y = myPane.CurveList[i].Points[1].Y + movey;



                            }
                        }
                    }
                    else
                    {
                        double xx1 = 0, yy1 = 0, xx2 = 0, yy2 = 0;
                        if (node == 1)
                        {
                            xx1 = x1;
                            yy1 = y1;
                        }
                        if (node == 2)
                        {
                            xx2 = x1;
                            yy1 = y1;
                        }
                        if (node == 3)
                        {
                            xx2 = x1;
                            yy2 = y1;
                        }
                        if (node == 3)
                        {
                            xx2 = x1;
                            yy2 = y1;
                        }
                        if (node == 4)
                        {
                            xx1 = x1;
                            yy2 = y1;
                        }
                        int count = 0;
                        for (int i = 0; i <= myPane.CurveList.Count; i++)
                        {
                            if (i < 4 * Num && i >= 4 * (Num - 1))
                            {
                                count++;
                                if (count == 1)
                                {
                                    myPane.CurveList[i].Points[0].X = xx1 == 0 ? myPane.CurveList[i].Points[0].X : xx1;
                                    myPane.CurveList[i].Points[0].Y = yy1 == 0 ? myPane.CurveList[i].Points[0].Y : yy1;
                                    myPane.CurveList[i].Points[1].X = xx2 == 0 ? myPane.CurveList[i].Points[1].X : xx2;
                                    myPane.CurveList[i].Points[1].Y = yy1 == 0 ? myPane.CurveList[i].Points[1].Y : yy1;
                                }
                                if (count == 2)
                                {
                                    myPane.CurveList[i].Points[0].X = xx2 == 0 ? myPane.CurveList[i].Points[0].X : xx2;
                                    myPane.CurveList[i].Points[0].Y = yy1 == 0 ? myPane.CurveList[i].Points[0].Y : yy1;
                                    myPane.CurveList[i].Points[1].X = xx2 == 0 ? myPane.CurveList[i].Points[1].X : xx2;
                                    myPane.CurveList[i].Points[1].Y = yy2 == 0 ? myPane.CurveList[i].Points[1].Y : yy2;
                                }
                                if (count == 3)
                                {
                                    myPane.CurveList[i].Points[0].X = xx2 == 0 ? myPane.CurveList[i].Points[0].X : xx2;
                                    myPane.CurveList[i].Points[0].Y = yy2 == 0 ? myPane.CurveList[i].Points[0].Y : yy2;
                                    myPane.CurveList[i].Points[1].X = xx1 == 0 ? myPane.CurveList[i].Points[1].X : xx1;
                                    myPane.CurveList[i].Points[1].Y = yy2 == 0 ? myPane.CurveList[i].Points[1].Y : yy2;
                                }
                                if (count == 4)
                                {
                                    myPane.CurveList[i].Points[0].X = xx1 == 0 ? myPane.CurveList[i].Points[0].X : xx1;
                                    myPane.CurveList[i].Points[0].Y = yy2 == 0 ? myPane.CurveList[i].Points[0].Y : yy2;
                                    myPane.CurveList[i].Points[1].X = xx1 == 0 ? myPane.CurveList[i].Points[1].X : xx1;
                                    myPane.CurveList[i].Points[1].Y = yy1 == 0 ? myPane.CurveList[i].Points[1].Y : yy1;
                                }

                            }
                        }
                    }



                    zedGraphControl.AxisChange();
                    zedGraphControl.Refresh();
                }
            }
            return false;
        }
        //鼠标点击
        public bool ZedMouseEventHandler(ZedGraphControl sender, System.Windows.Forms.MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                // 检查鼠标位置是否在图形对象内

                // 将屏幕坐标转换为图像坐标
                sender.GraphPane.ReverseTransform(new PointF(e.X, e.Y), out oldx, out oldy);
                Num = isinside(sender.GraphPane.CurveList, oldx, oldy);
                if (Num > 0)//点击在判断框内
                {
                    isDragging = true;


                }

            }
            return false;

        }
        //判断一个点是否在矩形内部
        public static int isinside(CurveList curveitems, double x, double y)
        {
            //4个一循环
            int count = 0;
            for (int i = 0; i <= curveitems.Count; i++)
            {
                count++;
                if (count == 4)
                {
                    count = 0;
                    var points = curveitems[i - 3].Points;
                    X1 = 0; X2 = 0; Y1 = 0; Y2 = 0;


                    X1 = points[0].X;
                    Y1 = points[0].Y;
                    X2 = points[1].X;
                    Y2 = curveitems[i - 2].Points[1].Y;

                    node = 0;
                    if (Math.Abs(x - X1) < 5)
                    {
                        if (Math.Abs(y - Y1) < 5)
                        {
                            node = 1;
                        }
                        if (Math.Abs(y - Y2) < 5)
                        {
                            node = 4;
                        }
                    }
                    if (Math.Abs(x - X2) < 5)
                    {
                        if (Math.Abs(y - Y1) < 5)
                        {
                            node = 2;
                        }
                        if (Math.Abs(y - Y2) < 5)
                        {
                            node = 3;
                        }
                    }

                    if ((x > X1 && x < X2 && y > Y1 && y < Y2) || node != 0)
                    {
                        return (i + 1) / 4;
                    }
                }
            }
            return 0;
        }
        //鼠标松开
        private bool zedGraphControl1_MouseUpEvent(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            isDragging = false;
            //存储到数据库

            return isDragging;
        }
        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {

            ZedHeight.Height = new GridLength(this.ActualWidth * 0.42, GridUnitType.Pixel);

        }

        private void dataGrid_RowEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {

        }
    }
}
